/*
 *  Copyright (c) 2010 Brian Stanek <bstanek@gmail.com>
 *
 *  Permission is hereby granted, free of charge, to any person
 *  obtaining a copy of this software and associated documentation
 *  files (the "Software"), to deal in the Software without
 *  restriction, including without limitation the rights to use,
 *  copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following
 *  conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *  OTHER DEALINGS IN THE SOFTWARE.
 */

package guice
{

import org.flexunit.assertThat;
import org.hamcrest.object.equalTo;
import flash.events.Event;
import flash.events.IEventDispatcher;

/**
 *  Test cases for guice.Key.
 */
public class KeyTest
{
    [Test]
    public function initializeWithClass():void
    {
        var key:Key = new Key(Event);
        assertThat(key.getAnnotation(), equalTo(""));
        assertThat(key.getHashCode(), equalTo("flash.events::Event"));
        assertThat(key.getType(), equalTo(Event));
        assertThat(key.toString(), equalTo("Key[type=flash.events::Event, annotation=]"));
    }

    [Test]
    public function initializeWithClassAndAnnotation():void
    {
        var key:Key = new Key(Event, "enterFrame");
        assertThat(key.getAnnotation(), equalTo("enterFrame"));
        assertThat(key.getHashCode(), equalTo("flash.events::Event|enterFrame"));
        assertThat(key.getType(), equalTo(Event));
        assertThat(key.toString(), equalTo("Key[type=flash.events::Event, annotation=enterFrame]"));
    }

    [Test]
    public function initializeWithString():void
    {
        var key:Key = new Key("flash.events::Event");
        assertThat(key.getAnnotation(), equalTo(""));
        assertThat(key.getHashCode(), equalTo("flash.events::Event"));
        assertThat(key.getType(), equalTo(Event));
        assertThat(key.toString(), equalTo("Key[type=flash.events::Event, annotation=]"));
    }

    [Test]
    public function initializeWithStringAndAnnotation():void
    {
        var key:Key = new Key("flash.events::Event", "enterFrame");
        assertThat(key.getAnnotation(), equalTo("enterFrame"));
        assertThat(key.getHashCode(), equalTo("flash.events::Event|enterFrame"));
        assertThat(key.getType(), equalTo(Event));
        assertThat(key.toString(), equalTo("Key[type=flash.events::Event, annotation=enterFrame]"));
    }

    [Test]
    public function equals():void
    {
        var key:Key = new Key(Event, "customEvent");
        var key2:Key = new Key("flash.events::Event", "customEvent");
        assertThat(key.equals(key2), equalTo(true));
    }

    [Test]
    public function keyInstanceIsEqualToItself():void
    {
        var key:Key = new Key(Event);
        assertThat(key.equals(key), equalTo(true));
    }

    [Test]
    public function equalsRequiresKeyInstance():void
    {
        var key:Key = new Key(Event);
        // Any type other than a Key instance should return false
        assertThat(key.equals(Key), equalTo(false));
    }

    [Test]
    public function equalsRequiresMatchingTypes():void
    {
        var key:Key = new Key(Event, "customEvent");
        var key2:Key = new Key(IEventDispatcher, "customEvent");
        assertThat(key.equals(key2), equalTo(false));
    }

    [Test]
    public function equalsRequiresMatchingAnnotations():void
    {
        var key:Key = new Key(Event, "annotation1");
        var key2:Key = new Key(Event, "annotation2");
        assertThat(key.equals(key2), equalTo(false));
    }

    [Test(expects="Error")]
    public function invalidTypeThrowsError():void
    {
        var key:Key = new Key(new Object());
    }

}

}
